<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 Thomas Breuss <thomas.breuss@zephir.ch>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(t3lib_extMgm::extPath('zboard') . 'class.tx_zboard_controller.php');


/**
 * Plugin 'zBoard - Categories' for the 'zboard' extension.
 *
 * @author	Thomas Breuss <thomas.breuss@zephir.ch>
 * @package	TYPO3
 * @subpackage	tx_zboard
 */
class tx_zboard_pi1 extends tx_zboard_controller
{
	var $prefixId      = 'tx_zboard_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_zboard_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'zboard';	// The extension key.
	var $pi_checkCHash = true;

    /**
     * Uebersteuert tx_zlib_controller::getActionMethodName()
     *
     * @return string
     */
    public function getAction()
    {
        $action = $this->getParam('action', '');
        if(empty($action)) {
            $action = $this->getFlexformValue('action');
        }
        return empty($action) ? 'default' : $action;
    }

    /**
     * Default
     */
    public function defaultAction()
    {
    }

    /**
     * Categories
     */
    public function categoriesAction()
    {
        $helper = $this->getHelper();
        $gateway = new tx_zboard_gateway();
        $gateway->setAuthorField($helper->config('authorField'));
        $categories = $gateway->fetchCategories();
        $this->view->categories = $categories;
        $this->render('categories');
    }

    /**
     * Forums
     */
	public function forumsAction()
	{
        $helper = $this->getHelper();
	
        $gateway = new tx_zboard_gateway();
        $gateway->setAuthorField($helper->config('authorField'));

        $forums = $gateway->fetchForums();

        $this->view->forums = $forums;
        $this->view->rootline = $gateway->fetchRootline($helper->config('rootPid'));
        $this->render('forums');
	}

    /**
     * Topics
     */
	public function topicsAction()
    {
        require_once(t3lib_extMgm::extPath('zlib') . '3rdparty/Zend/Paginator.php');

        $page = intval($this->getParam('page', 1));
        $helper = $this->getHelper();

        $gateway = new tx_zboard_gateway();
        $gateway->setAuthorField($helper->config('authorField'));

        $topics = $gateway->fetchTopics(false);

        $paginator = Zend_Paginator::factory($topics);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($helper->config('pagingTopicsPerPage'));
        $paginator->setPageRange($helper->config('pagingPageRange'));

        $this->view->paginator = $paginator;
        $this->view->topics = new tx_zboard_topics($paginator->getCurrentItems());
        $this->view->rootline = $gateway->fetchRootline($helper->config('rootPid'));
        $this->view->page = $page;

        $this->render('topics');
    }

    /**
     * Topic
     */
    public function topicAction()
    {
        $page = intval($this->getParam('page', 1));
        $uid = intval($this->getParam('uid', 0));
        if($uid > 0) {
            $helper = $this->getHelper();

            $gateway = new tx_zboard_gateway();
            $gateway->setAuthorField($helper->config('authorField'));

            $posts = $gateway->fetchPosts($uid, false);
            require_once(t3lib_extMgm::extPath('zlib') . '3rdparty/Zend/Paginator.php');
            $paginator = Zend_Paginator::factory($posts);
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage($helper->config('pagingPostsPerPage'));
            $paginator->setPageRange($helper->config('pagingPageRange'));

            $this->view->topic = $gateway->fetchTopic($uid);
            $this->view->paginator = $paginator;
            $this->view->posts = new tx_zboard_posts($paginator->getCurrentItems());

            $this->view->rootline = $gateway->fetchRootline($helper->config('rootPid'));
            $this->view->page = $page;

            $this->render('topic');

        }

    }

    /**
     * User profile
     */
    public function profileAction()
    {
        $uid = intval($this->getParam('uid', 0));

        if($uid > 0) {
            $helper = $this->getHelper();
        
            $gateway = new tx_zboard_gateway();
            $gateway->setAuthorField($helper->config('authorField'));

            $profile = $gateway->fetchProfile($uid);

            $this->view->profile = $profile;
            $this->view->rootline = $gateway->fetchRootline($helper->config('rootPid'));

            $this->render('profile');

        }

    }

}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zboard/pi1/class.tx_zboard_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zboard/pi1/class.tx_zboard_pi1.php']);
}

?>